<?php
/**
 *
 */
abstract class TextUtils {
	
	/**
	 * Cut long text up to shorter ones without breaking any words.
	 *
	 * @param 	String 	$str		Input string
	 * @param 	int 	$maxLength	Maximum amount of words
	 * @return 	String 	$str
	 */
	public static function cutText($str, $maxLength){
		if (!is_string($str)) {
			return NULL;
		}
		if (!is_numeric($maxLength)) {
			return NULL;
		}
		if (strlen($str) > $maxLength){
			$str = substr($str, 0, $maxLength);
			$pos = strrpos($str, " ");
			if($pos === false) {
				return substr($str, 0, $maxLength) . "...";
			}
			return substr($str, 0, $pos) . "...";
		} else {
			return $str;
		}
	}
	
	/**
	 * Generate a random string.
	 *
	 * @param 	int 	$length		Length of the key
	 * @return 	String	$key
	 */
	public static function randomString($length) {
		if (!is_numeric($length)) {
			return NULL;
		}
		$numeric = range("0", "9");
		$chars = range("a", "z");
		$capitalChars = range("A", "Z");
		$chars = array_merge($numeric, $chars, $capitalChars);
		for($i = 0 ; $i < $length ; $i++) {
			$key .= $chars[rand(0, count($aChrs) -1)];
		}
		return $key;
	}
	
	/**
	 * Check if an email address is valid or not.
	 * 
	 * @param 	String 	$email	The 'dirty' email address
	 * @return 	Boolean
	 */
	public static function validateEmail($email) {
		return preg_match("/^([!#-'*+\/-9=?^-~-]+)(?>\.(?1))*@(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?\.){1,2}[a-z]{2,6}$/iD", $email);	
	}
}
?>